#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int remove_title(xcb_connection_t *conn,xcb_screen_t * screen, xcb_window_t main_win){
	  // 去掉窗口装饰（如标题栏）
                    xcb_intern_atom_cookie_t hints_cookie = xcb_intern_atom(conn, 0, strlen("_MOTIF_WM_HINTS"), "_MOTIF_WM_HINTS");
                    xcb_intern_atom_reply_t *hints_reply = xcb_intern_atom_reply(conn, hints_cookie, NULL);
                    if (hints_reply) {
                        struct {
                            uint32_t flags;
                            uint32_t functions;
                            uint32_t decorations;
                            int32_t input_mode;
                            uint32_t status;
                        } motif_hints = {2, 0, 0, 0, 0}; // flags=2, decorations=0
                        xcb_change_property(conn, XCB_PROP_MODE_REPLACE, main_win,
                                            hints_reply->atom, hints_reply->atom, 32,
                                            sizeof(motif_hints) / 4, &motif_hints);
                        free(hints_reply);
                    }
}

int cancel_maxmium(xcb_connection_t *conn,xcb_screen_t * screen, xcb_window_t main_win){
      // 取消窗口最大化（移除 _NET_WM_STATE_MAXIMIZED_HORZ 和 _NET_WM_STATE_MAXIMIZED_VERT）
            xcb_intern_atom_cookie_t state_cookie = xcb_intern_atom(conn, 0, strlen("_NET_WM_STATE"), "_NET_WM_STATE");
            xcb_intern_atom_cookie_t max_horz_cookie = xcb_intern_atom(conn, 0, strlen("_NET_WM_STATE_MAXIMIZED_HORZ"), "_NET_WM_STATE_MAXIMIZED_HORZ");
            xcb_intern_atom_cookie_t max_vert_cookie = xcb_intern_atom(conn, 0, strlen("_NET_WM_STATE_MAXIMIZED_VERT"), "_NET_WM_STATE_MAXIMIZED_VERT");

            xcb_intern_atom_reply_t *state_atom = xcb_intern_atom_reply(conn, state_cookie, NULL);
            xcb_intern_atom_reply_t *max_horz_atom = xcb_intern_atom_reply(conn, max_horz_cookie, NULL);
            xcb_intern_atom_reply_t *max_vert_atom = xcb_intern_atom_reply(conn, max_vert_cookie, NULL);

            if (state_atom && max_horz_atom && max_vert_atom) {
                xcb_client_message_event_t ev = {0};
                ev.response_type = XCB_CLIENT_MESSAGE;
                ev.format = 32;
                ev.window = main_win;
                ev.type = state_atom->atom;
                ev.data.data32[0] = 0; // _NET_WM_STATE_REMOVE
                ev.data.data32[1] = max_horz_atom->atom;
                ev.data.data32[2] = max_vert_atom->atom;
                ev.data.data32[3] = 1;
                ev.data.data32[4] = 0;
                xcb_send_event(conn, 0, screen->root,
                    XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
                    (const char *)&ev);
	    }
}

int main() {
    // 连接到X服务器
    xcb_connection_t *conn = xcb_connect(NULL, NULL);
    if (xcb_connection_has_error(conn)) {
        fprintf(stderr, "Cannot open display\n");
        return -1;
    }

    // 获取默认屏幕
    const xcb_setup_t *setup = xcb_get_setup(conn);
    xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
    xcb_screen_t *screen = iter.data;

    // 创建主窗口
    xcb_window_t main_win = xcb_generate_id(conn);
    uint32_t value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
    uint32_t value_list[] = {screen->white_pixel, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS};
    xcb_create_window(conn,
                      XCB_COPY_FROM_PARENT,
                      main_win,
                      screen->root,
                      0, 0, 1920, 1280, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      screen->root_visual,
                      value_mask, value_list);

    // 设置窗口标题
    const char *title = "Main Window";
    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, main_win,
                        XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
                        strlen(title), title);

    // 创建子窗口
    xcb_window_t child_win = xcb_generate_id(conn);
    uint32_t child_value_list[] = {screen->black_pixel, XCB_EVENT_MASK_EXPOSURE};
    xcb_create_window(conn,
                      XCB_COPY_FROM_PARENT,
                      child_win,
                      main_win, // 父窗口
                      0, 0, 200, 150, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      screen->root_visual,
                      value_mask, child_value_list);

    /*uint32_t value_mask2 =  XCB_CW_EVENT_MASK;
    xcb_window_t child_win2 = xcb_generate_id(conn);
    uint32_t child_value_list2[] = { XCB_EVENT_MASK_EXPOSURE};
    xcb_create_window(conn,
                      XCB_COPY_FROM_PARENT,
                      child_win2,
                      main_win, // 父窗口
                      10, 10, 200, 150, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      screen->root_visual,
                      value_mask2, child_value_list2);

    // 设置 _NET_WM_WINDOW_OPACITY 属性为 0（完全透明）
                        xcb_intern_atom_cookie_t opacity_cookie = xcb_intern_atom(conn, 0, strlen("_NET_WM_WINDOW_OPACITY"), "_NET_WM_WINDOW_OPACITY");
                        xcb_intern_atom_reply_t* opacity_atom = xcb_intern_atom_reply(conn, opacity_cookie, NULL);
                        if (opacity_atom) {
                            uint32_t opacity = 0; // 0 = 完全透明, 0xffffffff = 不透明
                            xcb_change_property(
                                conn,
                                XCB_PROP_MODE_REPLACE,
                                child_win2,
                                opacity_atom->atom,
                                XCB_ATOM_CARDINAL,
                                32,
                                1,
                                &opacity
                            );
                            free(opacity_atom);
                        }
    xcb_map_window(conn,child_win2);
	*/

    // 显示窗口
    xcb_map_window(conn, main_win);
    xcb_map_window(conn, child_win);

    // 创建红色GC用于子窗口
    xcb_gcontext_t red_gc = xcb_generate_id(conn);
    uint32_t red_pixel = 0x000000; // 24位红色
    xcb_colormap_t colormap = screen->default_colormap;
    xcb_alloc_color_reply_t *color_reply = xcb_alloc_color_reply(
        conn,
        xcb_alloc_color(conn, colormap, 0x0000, 0, 0),
        NULL
    );
    if (color_reply) {
        red_pixel = color_reply->pixel;
        free(color_reply);
    }
    uint32_t gc_values[] = { red_pixel };
    xcb_create_gc(conn, red_gc, child_win, XCB_GC_FOREGROUND, gc_values);


    xcb_flush(conn);

    // 事件循环
    xcb_generic_event_t *event;
    while ((event = xcb_wait_for_event(conn))) {
        switch (event->response_type & ~0x80) {
            case XCB_EXPOSE: {
                xcb_expose_event_t *expose = (xcb_expose_event_t *)event;
                if (expose->window == child_win) {
                    // 填充子窗口为红色
                    xcb_rectangle_t rect = {0, 0, 200, 150};
                    xcb_poly_fill_rectangle(conn, child_win, red_gc, 1, &rect);
                    xcb_flush(conn);
                }
                break;
            }
            case XCB_KEY_PRESS:
		remove_title(conn, screen,main_win);
		cancel_maxmium(conn, screen,main_win);
		xcb_configure_window(conn, main_win, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, (uint32_t[]){200, 150});
		xcb_flush(conn);
                //free(event);
                //goto end;
        }
        free(event);
    }

end:
    xcb_disconnect(conn);
    return 0;
}
